package com.op.teachingliteapp.sender.ui.view.zoomgallery;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.widget.Gallery;

/**
 * Created by Genji on 2015/6/29.
 */
public class ZoomGallery extends Gallery implements ScaleGestureDetector.OnScaleGestureListener {
    ZoomGallery.OnScaleListener ScaleListener = null;
    ZoomGallery.OnSlideNextListener SlideNextListener = null;
    ZoomGallery.OnSlidePreListener SlidePreListener = null;
    float baseSpan = 0.0F;
    ZoomGallery.OnDoubleTapListener doubletapListener = null;
    float flingVelocityX = 0.0F;
    float flingVelocityY = 0.0F;
    private ZoomImageView imageView;
    boolean isFling = false;
    int kEvent;
    private int mGalleryHeight = 0;
    private int mGalleryWidth = 0;
    float originalScale = 0.0F;
    private ScaleGestureDetector scaleGestureScanner;
    ZoomGallery.OnTapListener tapListener = null;
    ZoomGallery.onZoomImageViewScrollListener zoomImageScrollListener = null;

    public ZoomGallery(Context var1) {
        super(var1);
    }

    public ZoomGallery(Context var1, AttributeSet var2) {
        super(var1, var2);
        this.scaleGestureScanner = new ScaleGestureDetector(var1, this);
    }

    public ZoomGallery(Context var1, AttributeSet var2, int var3) {
        super(var1, var2, var3);
    }

    private boolean isScrollingLeft(MotionEvent var1, MotionEvent var2) {
        return var2.getX() > var1.getX();
    }

    public int getGalleryHeight() {
        return this.mGalleryHeight;
    }

    public int getGalleryWidth() {
        return this.mGalleryWidth;
    }

    public boolean onDown(MotionEvent var1) {
        return super.onDown(var1);
    }

    public boolean onFling(MotionEvent var1, MotionEvent var2, float var3, float var4) {
        if (Math.abs(var3) >= (float) (this.mGalleryWidth / 3) && var1.getPointerCount() < 2 && var2.getPointerCount() < 2) {
            if (this.isScrollingLeft(var1, var2)) {
                this.kEvent = 21;
            } else {
                this.kEvent = 22;
            }

            this.flingVelocityX = var3;
            this.flingVelocityY = var4;
            View var5 = this.getSelectedView();
            if (var5 instanceof ZoomImageView) {
                this.imageView = (ZoomImageView) var5;
                float var6 = this.imageView.getScale() * (float) this.imageView.getImageWidth();
                float var7 = this.imageView.getScale() * (float) this.imageView.getImageHeight();
                float[] var8 = new float[9];
                this.imageView.getImageMatrix().getValues(var8);
                float var9 = var8[5];
                float var10 = var8[2];
                float var11 = var9 + var7;
                float var12 = var10 + var6;
                float var13 = ((float) this.mGalleryHeight - var7) / 2.0F;
                if ((int) var6 > this.mGalleryHeight && (int) var7 > this.mGalleryHeight) {
                    if (var9 > 0.0F) {
                        this.imageView.postTranslateDur(-var9, 200.0F);
                    } else if (var11 < (float) this.mGalleryHeight) {
                        this.imageView.postTranslateDur((float) this.mGalleryHeight - var11, 200.0F);
                    } else {
                        float var15 = var3 / 6.0F;
                        float var16 = var4 / 6.0F;
                        if ((var12 + var15 < (float) this.mGalleryWidth || var10 + var15 > 0.0F) && Math.abs(var3) > 100.0F) {
                            return this.onKeyDown(this.kEvent, (KeyEvent) null);
                        }

                        this.imageView.postTranslateXY(var15, var16);
                    }
                } else {
                    float var14 = var3 / 10.0F;
                    if ((var12 + var14 < (float) this.mGalleryWidth || var10 + var14 > 0.0F) && Math.abs(var3) > 100.0F) {
                        return this.onKeyDown(this.kEvent, (KeyEvent) null);
                    }

                    if (var9 < var13) {
                        this.imageView.postTranslateDur(var13 - var9, 200.0F);
                    } else if (var11 > (float) this.mGalleryHeight - var13) {
                        this.imageView.postTranslateDur(var13 - var9, 200.0F);
                    }
                }
            }

            return true;
        } else {
            return false;
        }
    }

    protected void onFocusChanged(boolean var1, int var2, Rect var3) {
        super.onFocusChanged(var1, var2, var3);
    }

    public void onLongPress(MotionEvent var1) {
        if (this.tapListener != null) {
            this.tapListener.onSingleTapConfirm(var1);
        }

        super.onLongPress(var1);
    }

    public boolean onScale(ScaleGestureDetector var1) {
        View var2 = this.getSelectedView();
        float var3 = var1.getCurrentSpan() / this.baseSpan;
        float var4 = var1.getFocusX();
        float var5 = var1.getFocusY();
        if (this.ScaleListener != null) {
            this.ScaleListener.onScale();
        }

        if (var2 instanceof ZoomImageView) {
            this.imageView = (ZoomImageView) var2;
            this.imageView.getImageMatrix();
            float var10000 = var3 / this.baseSpan;
            if (var3 * this.originalScale >= this.imageView.getBasedScaleRate()) {
                this.imageView.zoomTo(var3 * this.originalScale, var4, var5, true);
                return true;
            } else {
                this.imageView.zoomTo(this.imageView.getBasedScaleRate(), var4, var5, true);
                return true;
            }
        } else {
            return false;
        }
    }

    public boolean onScaleBegin(ScaleGestureDetector var1) {
        View var2 = this.getSelectedView();
        if (this.ScaleListener != null) {
            this.ScaleListener.onScaleBegin();
        }

        if (var2 instanceof ZoomImageView) {
            this.imageView = (ZoomImageView) var2;
            this.originalScale = this.imageView.getScale();
            this.baseSpan = var1.getCurrentSpan();
        }

        return true;
    }

    public void onScaleEnd(ScaleGestureDetector var1) {
        if (this.ScaleListener != null) {
            this.ScaleListener.onScaleEnd(this.imageView.getScale());
        }

    }

    public boolean onScroll(MotionEvent var1, MotionEvent var2, float var3, float var4) {
        this.zoomImageScrollListener.onScroll(var1, var2, var3, var4);
        View var6 = this.getSelectedView();
        if (var6 instanceof ZoomImageView) {
            this.imageView = (ZoomImageView) var6;
            float[] var8 = new float[9];
            this.imageView.getImageMatrix().getValues(var8);
            float var9 = var1.getX() - var2.getX();
            float var10 = this.imageView.getScale() * (float) this.imageView.getImageWidth();
            float var11 = this.imageView.getScale() * (float) this.imageView.getImageHeight();
            if ((int) var10 <= this.mGalleryWidth && (int) var11 <= this.mGalleryHeight) {
                super.onScroll(var1, var2, var3, var4);
            } else {
                float var12 = var8[2];
                float var13 = var8[5];
                float var14 = var12 + var10;
                float var15 = var13 + var11;
                float var16 = -var3;
                float var17 = -var4;
                float var18 = var11 - (float) this.mGalleryHeight;
                float var19 = var10 - (float) this.mGalleryWidth;
                if (var14 < (float) this.mGalleryWidth && var15 > (float) this.mGalleryHeight) {
                    if (Math.abs(var9) > (float) (this.mGalleryWidth / 3)) {
                        var16 = 0.0F;
                        var17 = 0.0F;
                        super.onScroll(var1, var2, var3, var4);
                    } else {
                        var16 = 0.0F;
                    }
                } else if (var16 > 0.0F) {
                    if (var12 >= 0.0F) {
                        float var23;
                        int var22 = (var23 = Math.abs(var9) - (float) (this.mGalleryWidth / 3)) == 0.0F ? 0 : (var23 < 0.0F ? -1 : 1);
                        var16 = 0.0F;
                        if (var22 > 0) {
                            super.onScroll(var1, var2, var3, var4);
                            var16 = 0.0F;
                        }
                    } else if (var12 + var16 >= 0.0F) {
                        var16 = 0.0F - var12;
                    }
                } else if (var16 < 0.0F) {
                    if (var12 <= -var19) {
                        float var24;
                        int var20 = (var24 = Math.abs(var9) - (float) (this.mGalleryWidth / 3)) == 0.0F ? 0 : (var24 < 0.0F ? -1 : 1);
                        var16 = 0.0F;
                        if (var20 > 0) {
                            super.onScroll(var1, var2, var3, var4);
                            var16 = 0.0F;
                        }
                    } else if (var12 + var16 <= -var19) {
                        var16 = -var19 - var12;
                    }
                }

                if (var17 > 0.0F) {
                    if (var13 >= 0.0F) {
                        var17 = 0.0F;
                    } else if (var13 + var17 >= 0.0F) {
                        var17 = 0.0F - var13;
                    }
                } else if (var17 < 0.0F) {
                    if (var13 <= -var18) {
                        var17 = 0.0F;
                    } else if (var13 + var17 <= -var18) {
                        var17 = -var18 - var13;
                    }
                }

                this.imageView.postTranslate(var16, var17);
            }
        } else {
            super.onScroll(var1, var2, var3, var4);
        }

        return false;
    }

    public boolean onSingleTapUp(MotionEvent var1) {
        if (this.tapListener != null) {
            this.tapListener.onSingleTapConfirm(var1);
        }

        return super.onSingleTapUp(var1);
    }

    public boolean onTouchEvent(MotionEvent var1) {
        this.scaleGestureScanner.onTouchEvent(var1);
        switch (var1.getAction()) {
            case 0:
                this.isFling = false;
            case 1:
            default:
                break;
            case 2:
                if (var1.getPointerCount() >= 2) {
                    return true;
                }
        }

        return super.onTouchEvent(var1);
    }

    public void setOnDoubleTapListener(ZoomGallery.OnDoubleTapListener var1) {
        this.doubletapListener = var1;
    }

    public void setOnScaleListener(ZoomGallery.OnScaleListener var1) {
        this.ScaleListener = var1;
    }

    public void setOnSingleTapListener(ZoomGallery.OnTapListener var1) {
        this.tapListener = var1;
    }

    public void setOnSlideNextListener(ZoomGallery.OnSlideNextListener var1) {
        this.SlideNextListener = var1;
    }

    public void setOnSlidePreListener(ZoomGallery.OnSlidePreListener var1) {
        this.SlidePreListener = var1;
    }

    public void setOnZoomImageViewScaleListener(ZoomGallery.onZoomImageViewScrollListener var1) {
        this.zoomImageScrollListener = var1;
    }

    public void setWidthHeight(int var1, int var2) {
        this.mGalleryWidth = var1;
        this.mGalleryHeight = var2;
    }

    public interface OnDoubleTapListener {
        boolean onDoubleTap(MotionEvent var1);
    }

    public interface OnLongPressListener {
        boolean onLongPress(MotionEvent var1);
    }

    public interface OnScaleListener {
        boolean onScale();

        boolean onScaleBegin();

        boolean onScaleEnd(float var1);
    }

    public interface OnSlideNextListener {
        boolean onSlideNext();
    }

    public interface OnSlidePreListener {
        boolean onSlidePre();
    }

    public interface OnTapListener {
        boolean onSingleTapConfirm(MotionEvent var1);
    }

    public interface OnTouchUpListener {
        void OnTouchUp(Rect var1, Bitmap var2);
    }

    public interface onZoomImageViewScrollListener {
        int onScroll(MotionEvent var1, MotionEvent var2, float var3, float var4);
    }
}
